from fastapi import APIRouter, Query
from typing import List, Optional
from datetime import datetime

router = APIRouter()

@router.get("/realtime")
async def get_realtime_data(sensor_type: Optional[str] = None):
    """获取实时传感器数据"""
    return {
        "sensors": [
            {
                "sensor_id": "S001",
                "type": "ph",
                "value": 6.8,
                "unit": "pH",
                "location": {"lat": 39.9042, "lng": 116.4074},
                "timestamp": datetime.now().isoformat()
            },
            {
                "sensor_id": "S002",
                "type": "moisture",
                "value": 24.5,
                "unit": "%",
                "location": {"lat": 39.9043, "lng": 116.4075},
                "timestamp": datetime.now().isoformat()
            }
        ]
    }

@router.get("/trend")
async def get_trend_data(
    start_date: Optional[str] = None,
    end_date: Optional[str] = None,
    plot_id: Optional[str] = None
):
    """获取历史趋势数据"""
    return {
        "labels": ["2024-11-05", "2024-11-06", "2024-11-07", "2024-11-08", "2024-11-09", "2024-11-10", "2024-11-11"],
        "datasets": [
            {
                "label": "pH值",
                "data": [6.5, 6.7, 6.9, 7.1, 6.8, 6.6, 6.5],
                "borderColor": "#409EFF"
            },
            {
                "label": "SOC (%)",
                "data": [2.3, 2.35, 2.4, 2.38, 2.36, 2.33, 2.35],
                "borderColor": "#67C23A"
            }
        ]
    }

@router.get("/heatmap")
async def get_ph_heatmap_data():
    """获取pH热力图数据"""
    return {
        "data": [
            {"lat": 39.9042, "lng": 116.4074, "ph": 6.8, "plot_id": "P001"},
            {"lat": 39.9043, "lng": 116.4075, "ph": 7.1, "plot_id": "P002"},
            {"lat": 39.9044, "lng": 116.4076, "ph": 6.5, "plot_id": "P003"},
            {"lat": 39.9045, "lng": 116.4077, "ph": 7.3, "plot_id": "P004"},
            {"lat": 39.9046, "lng": 116.4078, "ph": 6.9, "plot_id": "P005"}
        ]
    }
